//
//  HWGameDetailBannerCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/14.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameDetailBannerCell: UITableViewCell {
    var collectionView:UICollectionView!
    let photoBroseView = PYPhotoBrowseView()
    var model:HWGameDetailInfoModel?{
        didSet{
            self.collectionView.reloadData()
        }
    }
    var tempCell:HWGameDetailBannerPagerVideoCell!
    /// 播放
    var gameImageViewClick:((_:UIImageView,_:HWGameDetailInfoModel,_:HWGameDetailBannerPagerVideoCell)->())?
    /// 结束播放
    var endPlay:(()->())?
    
    var didClickBanner:((_ :String)->())?
    class func cellWithTableView(tableView:UITableView) -> (HWGameDetailBannerCell) {
        let reUse:String = "HWGameDetailBannerCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameDetailBannerCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameDetailBannerCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = .clear
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.minimumLineSpacing = 7*widthScale
        flowL.minimumInteritemSpacing = 7*widthScale
        flowL.scrollDirection = .horizontal
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(HWGameDetailBannerPagerVideoCell.self, forCellWithReuseIdentifier: "HWGameDetailBannerPagerVideoCell")
        collectionView.register(HWGameDetailBannerPagerCell.self, forCellWithReuseIdentifier: "HWGameDetailBannerPagerCell")
        contentView.addSubview(collectionView)
    }
    
    func initFrame() -> Void {
        collectionView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview().priority(.high)
            make.height.equalTo(216*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}

extension HWGameDetailBannerCell:UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        photoBroseView.imagesURL = model?.imgs.imgs
        photoBroseView.frameFormWindow = CGRect(x: screenWidth/2, y: screenHeight/2, width: 0, height: 0)
        photoBroseView.frameToWindow = CGRect(x: screenWidth/2, y: screenHeight/2, width: 0, height: 0)
        if (model?.imgs.video.video_url.count ?? 0) > 0 {
            if indexPath.row != 0 {
                photoBroseView.currentIndex = indexPath.row - 1
                photoBroseView.show()
            }else {
                
            }
        }else {
            photoBroseView.currentIndex = indexPath.row
            photoBroseView.show()
        }
        
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 15*widthScale, left: 15*widthScale, bottom: 30*widthScale, right: 15*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 8*widthScale
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if (model?.imgs.video.video_url.count ?? 0) > 0 {
            return (model?.imgs.imgs.count ?? 0) + 1
        } else {
            return model?.imgs.imgs.count ?? 0
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.row == 0 {
            if (model?.imgs.video.video_url.count ?? 0) > 0 {
                return CGSize(width: 304*widthScale, height: 171*widthScale)
            } else {
                return CGSize(width: 103*widthScale, height: 171*widthScale)
            }
        }else {
            return CGSize(width: 103*widthScale, height: 171*widthScale)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let videoCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWGameDetailBannerPagerVideoCell", for: indexPath) as! HWGameDetailBannerPagerVideoCell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWGameDetailBannerPagerCell", for: indexPath) as! HWGameDetailBannerPagerCell
        if indexPath.row == 0 {
            if (model?.imgs.video.video_url.count ?? 0) > 0 {
                videoCell.model = model
                videoCell.gameImageViewClick = {
                    self.gameImageViewClick?($0,$1,$2)
                    self.tempCell = $2
                }
                return videoCell
            }else {
                cell.gameImageView.sd_setImage(with: URL(string: model?.imgs.imgs[indexPath.row] ?? ""), completed: nil)
                return cell
            }
        }else {
            if (model?.imgs.video.video_url.count ?? 0) > 0 {
                cell.gameImageView.sd_setImage(with: URL(string: model?.imgs.imgs[indexPath.row - 1] ?? ""), completed: nil)
                return cell
            }else {
                cell.gameImageView.sd_setImage(with: URL(string: model?.imgs.imgs[indexPath.row] ?? ""), completed: nil)
                return cell
            }
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        if self.tempCell == cell {
            self.endPlay?()
            self.tempCell = nil
        }
    }
    
}
class HWGameDetailBannerPagerVideoCell:UICollectionViewCell {
    var gameImageView:UIImageView!
    var playerImageView:UIImageView!
    var videoPlayButton:UIButton!
    var model:HWGameDetailInfoModel?{
        didSet {
            gameImageView.sd_setImage(with: URL(string: model?.imgs.video.video_pic ?? ""), completed: nil)
        }
    }
    var gameImageViewClick:((_:UIImageView,_:HWGameDetailInfoModel,_:HWGameDetailBannerPagerVideoCell)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() -> Void {
        gameImageView = UIImageView()
        gameImageView.layer.cornerRadius = 8*widthScale
        gameImageView.layer.masksToBounds = true
        gameImageView.backgroundColor = .clear
        gameImageView.contentMode = .scaleAspectFill
        gameImageView.isUserInteractionEnabled = true
        contentView.addSubview(gameImageView)
        
        playerImageView = UIImageView()
        playerImageView.image = UIImage(named: "btn_video")
        gameImageView.addSubview(playerImageView)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(imageViewTap(_:)))
        gameImageView.addGestureRecognizer(tap)
    }
    
    private func initFrame() -> Void {
        gameImageView.snp.makeConstraints { make in
            //            make.left.top.equalTo(15*widthScale)
            //            make.right.equalToSuperview()
            //            make.bottom.equalTo(-30*widthScale)
            //            make.width.equalTo(304*widthScale)
            make.edges.equalToSuperview()
        }
        
        playerImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(40*widthScale)
        }
    }
    
    @objc func imageViewTap(_:UITapGestureRecognizer) -> Void {
        self.gameImageViewClick?(self.gameImageView,model ?? HWGameDetailInfoModel(),self)
    }
}
class HWGameDetailBannerPagerCell:UICollectionViewCell {
    var gameImageView:UIImageView!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() -> Void {
        gameImageView = UIImageView()
        gameImageView.layer.cornerRadius = 8*widthScale
        gameImageView.layer.masksToBounds = true
        gameImageView.backgroundColor = .clear
        gameImageView.contentMode = .scaleAspectFill
        addSubview(gameImageView)
        
        
        
    }
    
    private func initFrame() -> Void {
        gameImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        
    }
    
}
